<template>
	<view class="login-container">
		<view class="login-title">登录测试</view>
		<view class="button-group">
			<u-button type="primary" @click="handleParentLogin" class="login-btn">家长登录</u-button>
			<u-button type="success" @click="handleTeacherLogin" class="login-btn">老师登录</u-button>
			<u-button type="warning" @click="handlePrincipalLogin" class="login-btn">校长登录</u-button>
		</view>
	</view>
</template>

<script>
import { getUserRole } from '@/api/login';
export default {
	data() {
		return {

		}
	},
	methods: {
		// 清空所有session
		clearAllStorage() {
			try {
				uni.clearStorageSync();
			} catch (e) {
				console.log('清空session失败', e);
			}
		},
		handleParentLogin() {
			try {
				// 清空所有session
				this.clearAllStorage();
				
				// 存储新的session
				uni.setStorageSync('parentId', 1001);
				uni.setStorageSync('campusId', 1);
        uni.setStorageSync('role', 'parent');
				uni.showToast({
					title: '家长登录成功',
					icon: 'success'
				});
				uni.switchTab({
					url: '/pages/homepage/homepage'
				})
			} catch (e) {
				uni.showToast({
					title: '登录失败',
					icon: 'error'
				});
				console.log(e)
			}
		},
		handleTeacherLogin() {
			try {
				// 清空所有session
				this.clearAllStorage();
				
				// 存储新的session
				uni.setStorageSync('userId', 100);
				let userId = 100
				getUserRole(userId).then(res => {
					console.log(res, 123);
					uni.setStorageSync('role', res.data.userRole.roleKey);
					uni.setStorageSync('userName', res.data.userRole.userName);
					uni.setStorageSync('campusId', res.data.userRole.campusId);
					uni.switchTab({
						url: '/pages/homepage/homepage'
					})
				})

				uni.showToast({
					title: '老师登录成功',
					icon: 'success'
				});
				
				
			} catch (e) {
				uni.showToast({
					title: '登录失败',
					icon: 'error'
				});
				console.log(e)
			}
		},
		handlePrincipalLogin() {
			try {
				// 清空所有session
				this.clearAllStorage();
				
				// 存储新的session
				uni.setStorageSync('userId', 101); // 使用不同的use	rId
				let userId = 102
				getUserRole(userId).then(res => {
					console.log(res, 123);
					uni.setStorageSync('role', res.data.userRole.roleKey);
					uni.setStorageSync('userName', res.data.userRole.userName);
					uni.setStorageSync('campusId', res.data.userRole.campusId);
				})

				uni.showToast({
					title: '校长登录成功',
					icon: 'success'
				});
				
				uni.switchTab({
					url: '/pages/homepage/homepage'
				})
			} catch (e) {
				uni.showToast({
					title: '登录失败',
					icon: 'error'
				});
				console.log(e)
			}
		}
	}
}
</script>

<style>
.login-container {
	padding: 40rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	height: 100vh;
	background-color: #f5f5f5;
}

.login-title {
	font-size: 40rpx;
	font-weight: bold;
	margin-bottom: 60rpx;
	color: #333;
}

.button-group {
	width: 100%;
	display: flex;
	flex-direction: column;
	gap: 30rpx;
}

.login-btn {
	width: 100% !important;
	height: 90rpx !important;
	font-size: 32rpx !important;
}
</style>
